package org.bigjava.user.web.controller;

import org.apache.commons.lang3.StringUtils;
import org.bigjava.bean.Attention;
import org.bigjava.bean.User;
import org.bigjava.common.Constant;
import org.bigjava.user.service.impl.PostsServiceImpl;
import org.bigjava.user.service.impl.UserServicesImpl;
import org.bigjava.user.util.AESUtil;
import org.bigjava.user.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
@SessionAttributes("user")
public class UserController {

    AESUtil aesUtil = new AESUtil();
    DateUtil dateUtil = new DateUtil();
    @Autowired
    UserServicesImpl service;
    @Autowired
    PostsServiceImpl postsService;

//    @RequestMapping("qingyu")
//    public String qingyu(){
//        //跳转到轻语首页
//        return "qingyu";
//    }


    @RequestMapping("index2")
    public String index2(){
        return "index";
    }
    @RequestMapping("login")
    public String login() {
        //跳转到登录页
        return "login";
    }

    @RequestMapping("sign")
    public String sign() {
        //跳转到注册页
        return "sign";
    }

    @RequestMapping("signuser")
    public String signs(User user) {
        //注册用户


        boolean sign = service.sign(user);
        System.out.println("signuser-------" + sign);
        if (sign) return "redirect:signok";
        else return "sign";

    }

    @RequestMapping("signok")
    public String signok() {
        //注册成功，跳转到登录页

        return "login";

    }


    @RequestMapping("loginuser")
    public ModelAndView loginUser(HttpSession session, User user) {
        ModelAndView modelAndView = new ModelAndView();
        //登录验证
        if (StringUtils.isNotBlank(user.getUserName()) && StringUtils.isNotBlank(user.getPassword())) {
            if (service.loginUser(user)) {//登录成功
                User u = service.getUserByName(user.getUserName());//查询用户资料
                if (service.queryUserState(u.getUserId()) == 2) {//状态为禁止登录
                    modelAndView.setViewName("login");
                    modelAndView.addObject("state", "该账号目前被禁止登录");
                    return modelAndView;
                }
                long day = dateUtil.getUserday(u.getUserDate());
                session.setAttribute(Constant.userid, u.getUserId());
                modelAndView.addObject(Constant.sessionUser, u);
                modelAndView.addObject("userDay", day);
                modelAndView.setViewName("index");
                //获取用户个性动态推荐
                List<Attention> personalityPosts = postsService.personalityPosts((long)session.getAttribute(Constant.userid));
                //传入前端
                session.setAttribute("personlityPosts",personalityPosts);
                return modelAndView;

            } else {//登录失败
                modelAndView.setViewName("login");
                return modelAndView;
            }
        } else {
            modelAndView.setViewName("login");
            return modelAndView;
        }

    }

    @RequestMapping("logout")
    public ModelAndView logout(HttpSession session, ModelAndView modelAndView) {
        session.invalidate();
        modelAndView.addObject(Constant.sessionUser, null);
//        session.removeAttribute(Constant.sessionUser);
//        System.out.println("注销"+session.getAttribute(Constant.sessionUser));
        modelAndView.setViewName("index");
        return modelAndView;
    }


    @RequestMapping("getUserByUserName")
    public ModelAndView getUserByUserName(User user) {
        //根据用户名查询用户资料
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("");
        modelAndView.clear();

        System.out.println("getUserByUserName");
        User u = service.getUserByName(user.getUserName());
        modelAndView.addObject(Constant.sessionUser, u);
        System.out.println(u);

        return modelAndView;
    }

    @RequestMapping("getAllUser")
    public void getAllUser() {
        //获取全部用户
        List<User> userList = service.getAllUser();
        for (User u : userList) System.out.println(u.getUserName());
    }

    @RequestMapping("updateUserlikes")
    public void updateUserlikes() {
        long userid = 1;
        System.out.println("like");
        service.updateUserlikes(userid);
    }

    @RequestMapping("mydata")
    public ModelAndView mydata(@ModelAttribute("user") User user, HttpSession session) {
        //跳转到我的资料页
        System.out.println("mydata");
        long id = (long) session.getAttribute("userId");
        ModelAndView modelAndView = new ModelAndView();
        System.out.println(session.getAttribute("userId"));
        User u = service.getUserById(id);
        long day = dateUtil.getUserday(u.getUserDate());//转换日期为注册天数
        modelAndView.addObject(Constant.sessionUser, u);
        modelAndView.addObject("day", day);
        modelAndView.setViewName("mydata");

        return modelAndView;
    }

    @RequestMapping("ajaxtest")
    public String ajaxtest() {
        return "myattention02";
    }


}

